﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Monhoc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] != null)
            {
                if (Session["quyen"].ToString() == "AD")
                {
                    Response.Redirect("DangnhapAdmin.aspx");
                }
                else
                {
                    if (Session["Tendangnhap"] != null && Session["Tendangnhap"] != "")
                    {

                        MasterPage mt = this.Master as MasterPage;
                        LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                        lb.Visible = false;
                        Label bl = mt.FindControl("lblDnthanhcong") as Label;
                        bl.Visible = true;
                        Label blten = mt.FindControl("lbtendn") as Label;
                        blten.Visible = true;
                        blten.Text = Session["Tendangnhap"].ToString();
                        LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                        lbDangky.Visible = false;
                        LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                        lbthoat.Visible = true;
                    }
                    if (!IsPostBack)
                    { napMonhoc(); }
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        private void napMonhoc()
        {
            try
            {
                griMonhoc.DataSource = new MonhocBL().SelectAll();
                griMonhoc.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        private void Lay_mon(string ma)//thuc hien chuc nang bind data 
        {
            MonhocDO obj = new MonhocDO();
            obj = new MonhocBL().Select(new MonhocDO { Mamonhoc = ma });
            try
            {

                txtmamon.Text = obj.Mamonhoc;
                txtmamon.Enabled = false;
                txtTenmon.Text = obj.Tenmon;
                //txtSochuong.Text = Convert.ToString(mBAL.Hienthimonhoc_theoma(ma).Rows[0].ItemArray[2]);
                drSochuong1.SelectedValue = obj.Sochuong;
            }

            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                obj = null;
            }
        }

        protected void griMonhoc_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string ma = Convert.ToString(e.CommandArgument);
            int result = 0;
            try
            {
                if (e.CommandName == "xoa")
                {
                    result = new CT_MonhocBL().Delete(new CT_MonhocDO { Mamonhoc = ma });
                    result = new MonhocBL().Delete(new MonhocDO { Mamonhoc = ma });

                    if (result == 0)
                    {
                        lblLoi.Text = "Xoa khong thang cong !";
                        
                    }
                    else
                    {
                        lblLoi.Text = "Xoa thanh cong !";
                        napMonhoc();
                    }
                }
                if (e.CommandName == "sua")
                {
                    pnMon.Visible = true;
                    Lay_mon(ma);
                    pnThem.Visible = false;
                }
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        protected void imgtk_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTimkiem.Text == "")
            {
                lblLoi.Text = "Bạn chưa nhập thông tin tìm kiếm";
            }
            else
            {
                string ma = txtTimkiem.Text;
                if (rd_Ykien.SelectedItem.Value == "0")
                {
                    Tim_kiem(0, ma);
                }
                if (rd_Ykien.SelectedItem.Value == "1")
                {
                    Tim_kiem(1, ma);
                }
            }
        }

        protected void imgRe_Click(object sender, ImageClickEventArgs e)
        {
            napMonhoc();
            pnThem.Visible = false;
            pnMon.Visible = false;
            lblLoi.Visible = false;
        }
        protected void btDongy_Click(object sender, EventArgs e)
        {
            MonhocDO obj = new MonhocDO();
            int kq = 0;
            obj.Mamonhoc = txtmamon.Text;
            obj.Tenmon = txtTenmon.Text;
            obj.Sochuong = drSochuong1.SelectedValue;
            try
            {
                kq = new MonhocBL().Update(obj);
                if (kq == 0)
                {
                    lblLoi.Text = "Update thanh cong !";
                    napMonhoc();
                    pnMon.Visible = false;
                }
                else
                {
                    lblLoi.Text = "Update khong thanh cong !";
                }
            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {
            }
        }
        private void Tim_kiem(int trangthai, string tukhoa)
        {
            try
            {
                griMonhoc.DataSource = new MonhocBL().TimKiem(trangthai,tukhoa);
                griMonhoc.DataBind();
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        protected void btLamlai_Click(object sender, EventArgs e)
        {
            pnMon.Visible = false;
        }
        protected void imgThem_Click(object sender, ImageClickEventArgs e)
        {

            //NapGV();
            pnThem.Visible = true;
            pnMon.Visible = false;

        }

        protected void btLuu_Click(object sender, EventArgs e)
        {
            MonhocDO obj = new MonhocDO();
            int kq = 0;
            obj.Mamonhoc = txtma.Text;
            //int magv = Convert.ToInt32( drGV.SelectedValue);
            try
            {
                if (new MonhocBL().Select(obj).Tenmon != null)
                    lblLoi.Text = "Da ton tai ma mon!";
                else
                {
                    obj.Tenmon = txtmon.Text;
                    obj.Sochuong = drSochuong2.SelectedValue;
                    new MonhocBL().Insert(obj);
                    if (kq == 0)
                    {
                        lblLoi.Text = "Them thanh cong !";
                        //kq = mBAL.Them_gmon_gv(mamon, magv);
                        napMonhoc();
                        //pnYkien.Visible = false;

                    }
                    else
                    {
                        lblLoi.Text = "Them khong thanh cong !";
                    }
                }
            }
            catch (Exception ex)
            {
                lblLoi.Text = ex.Message;
            }
            finally
            {
            }

        }

        protected void btHuy_Click(object sender, EventArgs e)
        {
            pnThem.Visible = false;
        }

        protected void griMonhoc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            griMonhoc.PageIndex = e.NewPageIndex;
            this.napMonhoc();
        }
    }
}
